<template>
  <div>
    <el-container>
      <el-header>注册</el-header>
      <el-main>
        <div id="beg">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item
              label="真实姓名"
              prop="UsersName"
              style="margin-top: 10px"
            >
              <el-input
                v-model="ruleForm.UsersName"
                style="width: 200px"
              ></el-input>
            </el-form-item>

            <el-form-item label="用户名" prop="Admin">
              <el-input
                v-model="ruleForm.Admin"
                style="width: 200px"
              ></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="Pwd">
              <el-input v-model="ruleForm.Pwd" style="width: 200px"></el-input>
            </el-form-item>

            <el-form-item label="上传头像">
              <el-upload
                class="avatar-uploader"
                action="https://localhost:44353/api/LoginAPI/UpLod"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >立即注册</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        UsersName: "",
        Admin: "",
        Pwd: "",
        UserImg: "",
      },
      rules: {
        UsersName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" },
        ],
        Admin: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        Pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      imageUrl: "",
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("https://localhost:44353/api/LoginAPI/ZhuCe", this.ruleForm)
            .then((res) => {
              if (res.data > 0) {
                this.$message({
                  showClose: true,
                  message: "注册成功,请登录",
                  type: "success",
                });
                this.$router.push("/Long");
              } else {
                this.$message({
                  showClose: true,
                  message: "错了哦，注册失败",
                  type: "error",
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.UserImg = res;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
#beg {
  margin-left: auto;
  margin-right: auto;
  width: 350px;
  height: 500px;
  border-style: double;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>